home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / CGIshell 1.3.2 / shell / template.4th < prev   
Encoding:
Text File  |  1996-04-23  |  4.2 KB  |  142 lines  |  [TEXT/ALFA]

  1. \
  2. \
  3. \  PF Forms Handler:   Template  --  version 1.3.2
  4. \
  5. \
  6. \  (c) Ronald T. Kneusel, 1995, 1996
  7. \  (rkneusel@post.its.mcw.edu)
  8. \
  9. \  This code may be used and distributed freely provided the copyright 
  10. \  notice remains intact and my name is mentioned in the documentation.
  11. \
  12. \  Last mod: 23-Apr-96
  13. \  =========================================================================
  14. \
  15. \  This file assumes these files to be already loaded (in this order):
  16. \
  17. \    server.4th  -  web server interface
  18. \    field.4th   -  field definition and access words
  19. \
  20. \  This code handles the reading and evaluation of the template HTML file.
  21. \
  22. \  A template file is an external text file that contains the HTML source to 
  23. \  be returned by the CGI with named markers where calculated values are to 
  24. \  be substituted:
  25. \
  26. \  <h1>A Reply</h1><hr>
  27. \  You are: `name` <p>
  28. \  Age: `age` <p>
  29. \  Weight: `weight` <p>
  30. \
  31. \  The above will look for fields or named strings with the names "name", "age", 
  32. \  and "weight" and substitute their value in the reply string.
  33.  
  34. \ *** None of these words check for overflow or error conditions!  Memory is 
  35. \     at a premium, so you, the programmer, are on your own!
  36.  
  37.  
  38. 1024 constant buffSize        \ size of input buffer
  39. create rwbuff buffSize allot  \ the buffer
  40. variable ~i                   \ start by filling the buffer
  41. variable ~f                   \ number of bytes into the file
  42.  
  43. : fillbuff ( -- )  \ call this after OPEN to use the buffer
  44.    1024 ~i !  0 ~f !  rwbuff buffSize 0 fill ;
  45.  
  46. : getch ( -- c )  \ read a character from the buffer
  47.    ~i @ dup 1024 < IF
  48.      rwbuff + c@  1 ~i +!  1 ~f +!
  49.    ELSE
  50.      drop 0 ~i !  \ clear index
  51.      rwbuff buffSize 0 fill
  52.      @size  ~f @ - 3 -  dup 1 < IF
  53.        drop  -1  \ nothing to read, return -1
  54.      ELSE
  55.        buffSize  min  \ # bytes to read
  56.        rwbuff swap READ
  57.        rwbuff c@  1 ~i +!
  58.      THEN
  59.    THEN ;
  60.  
  61. ( string array words )
  62.  
  63. : array>> ( #elements --  )  \ create an array of #elements
  64.    create 2* allot ;
  65.  
  66. : !array ( data index array -- )  \ store in index
  67.    swap 2* + ! ;
  68.  
  69. : @array ( index array -- data )  \ get data in an array
  70.    swap 2* + @ ;
  71.  
  72. : >array ( 00 01 .. n array -- )  \ store entire array
  73.    swap -1 swap 1- DO
  74.      dup rot swap r swap !array
  75.    -1 +LOOP ;
  76.  
  77. \ add these to the number convert
  78.  
  79. : n>str ( n s -- )  \ integer to string
  80.    >r 0 d>f r> 0 fix f>str ;
  81.  
  82. : str>num ( s -- n )  \ string to integer
  83.    str>f f>d drop ;
  84.  
  85. variable ~output  \ output string
  86. variable ~fname   \ filename string
  87. variable ~length  \ hold length of output string
  88.  
  89. create !@#  32 allot  \ hold the name
  90. create #@!  0 , 0 ,   \ null string
  91.  
  92. : >!@# ( c -- )  \ append a character to !@#
  93.   !@# length  !@# + dup >r c!     \ character
  94.    0 r> 1+ c! ;  \ null
  95.  
  96. : token ( -- )  \ load !@# with name of token
  97.    0 !@# c!                       \ clear !@#
  98.    BEGIN 
  99.      getch dup 96 = 0=            \ while not ` (backquote)
  100.    WHILE  >!@#  REPEAT            \ append to !@#
  101.    drop
  102. ;
  103.  
  104. : lookup ( -- addr )  \ lookup the token name and return address of text part
  105.   #T# @ 0 DO
  106.     r @(T) .name !@#
  107.     $= IF
  108.       r @(T) .text \ got it
  109.       10000
  110.     ELSE 1 THEN
  111.   +LOOP
  112. ;
  113.  
  114. : >>output  ( c -- )  \ append a character
  115.   ~output @ ~length @ + dup >r c!  0 r> 1+ c! 1 ~length +! ; macro
  116.  
  117. : template ( output fname new|append -- )
  118.    \ process a template file
  119.    >r  ~fname !  ~output !  r>
  120.    IF 0 ~output @ c!  0 ~length !        \ zero the string
  121.    ELSE ~output @ length ~length ! THEN  \ append to string 
  122.    ~fname @ open                         \ open the file
  123.    fillbuff                              \ init input buffer
  124.    @size 0 DO   
  125.      getch                               \ get a character
  126.      dup 96 = IF                         \ check for ` (backquote)
  127.       drop                               \ lose the `
  128.       token                              \ get the name 
  129.       lookup                             \ and lookup the string
  130.       ~output @ strcpy                   \ and put it in
  131.       ~output @ length ~length !         \ adjust length
  132.       !@# length 1+                      \ length of name+1 (for backquote)
  133.      ELSE
  134.       dup 31 > IF
  135.        >>output                          \ append to output
  136.       ELSE drop THEN 1
  137.      THEN
  138.    +LOOP
  139.    close                                 \ close the file
  140. ;
  141.  
  142.